package com.mystyle.le.weekproject.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.mystyle.le.weekproject.R;
import com.mystyle.le.weekproject.adapter.ReaderAdapter;
import com.mystyle.le.weekproject.asynctaks.HeadListAsyncTaks;
import com.mystyle.le.weekproject.beans.DetialList;
import com.mystyle.le.weekproject.beans.ReaderPicture;
import com.mystyle.le.weekproject.intentfactbackcall.MyCallBack;
import com.mystyle.le.weekproject.utils.ParseJson;
import com.mystyle.le.weekproject.utils.Url;


import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*
* 观看漫画的Activity
* */
public class ReaderPicActivity extends AppCompatActivity {


    private ListView reader_LV;
    private List<ReaderPicture> pictureList = new ArrayList<>();
    private  List<DetialList> lists = new ArrayList<>();
    private ImageView reader_battery;
    private TextView read_time_TV;
    private TextView read_title_TV;
    private TextView read_countNum_TV;
    private int pageNum =0 ;
    private Button read_add;
    private boolean isLast = false;
    private   ReaderAdapter readerAdapter;
    private SimpleDateFormat dateFormat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reader_pic);
        /*
        * 初始化控件
        * */
        initView();
        //通过传过来的值，下载数据，更新UI
        Intent intent = getIntent();
        String read_id1 = intent.getStringExtra("Read_id1");
        String read_number = intent.getStringExtra("Read_number");
        //通过分类传递过来的值下载数据
        String detialsoft_id = intent.getStringExtra("detialsoft_id");


        initData(read_id1,read_number);
    }

    /*
    * 下载数据并更新UI
    * */
    private void initData(final String id, final String number) {
        //获取系统时间
        dateFormat = new SimpleDateFormat("HH:mm:ss");
       Date curDate = new Date(System.currentTimeMillis());
        String format = dateFormat.format(curDate);
        read_time_TV.setText(format);


        readerAdapter = new ReaderAdapter(pictureList, ReaderPicActivity.this);
       new HeadListAsyncTaks(new MyCallBack.HeadListBack() {
           @Override
           public void sendBack(String path) {
               List<ReaderPicture> readerPictures = ParseJson.parseReaderPic(path);
               if (readerPictures!=null&&readerPictures.size()>0){
                   pictureList.addAll(readerPictures);
                   ReaderPicture readerPicture = pictureList.get(pageNum);
                   String title = readerPicture.getTitle();
                   read_title_TV.setText(title);
                   String page = readerPicture.getPage();
                   read_countNum_TV.setText(page+"/"+pictureList.size());
               }
           }
       }, ReaderPicActivity.this).execute(Url.getChapterData(id,number));
        reader_LV.setAdapter(readerAdapter);
        addChange(id, number);
    }

    private void addChange(final String id, final String number) {
        //滑动事件，判断是否到达底部
        reader_LV.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (isLast&&scrollState==SCROLL_STATE_IDLE){
                    read_add.setVisibility(View.VISIBLE);
                }else {
                    read_add.setVisibility(View.GONE);
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                isLast = (firstVisibleItem+visibleItemCount)==totalItemCount;
            }
        });


        //点击看下一页
        read_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                pictureList.clear();
                pageNum++;
                new HeadListAsyncTaks(new MyCallBack.HeadListBack() {
                    @Override
                    public void sendBack(String path) {
                        List<ReaderPicture> readerPictures = ParseJson.parseReaderPic(path);
                        if (readerPictures!=null&&readerPictures.size()>0){
                            pictureList.addAll(readerPictures);
                            ReaderPicture readerPicture = pictureList.get(pageNum);
                            String title = readerPicture.getTitle();
                            read_title_TV.setText(title);
                            String page = readerPicture.getPage();
                            read_countNum_TV.setText(page+"/"+pictureList.size());
                        }
                    }
                }, ReaderPicActivity.this).execute(Url.getChapterData(id,number));

            }
        });
        readerAdapter.notifyDataSetChanged();
        reader_LV.setAdapter(readerAdapter);

    }

    /*
    * 初始化控件
    * */
    private void initView() {
        reader_LV = ((ListView) findViewById(R.id.reader_LV));
        reader_battery = ((ImageView) findViewById(R.id.reader_battery));//电量图
        read_time_TV = ((TextView) findViewById(R.id.read_time_TV));//时间
        read_title_TV = ((TextView) findViewById(R.id.read_title_TV));//章节数
        read_countNum_TV = ((TextView) findViewById(R.id.read_countNum_TV));//当前读的漫画页数
        read_add = ((Button) findViewById(R.id.read_add));
    }
}
